package org.simplesocket.simplenettycore.context;

import io.netty.channel.Channel;
import lombok.Getter;
import org.simplesocket.simplenettycore.config.SimpleNettyConfig;
import org.simplesocket.simplenettycore.route.Router;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Getter
public class SimpleNettyContext {
    
    private static final SimpleNettyContext simpleNettyContext = new SimpleNettyContext();
    
    // 所有添加了 @RouterEndpoint 的单例对象映射
    private final Map<String,Object> instances = new ConcurrentHashMap<>();
    
    // 已连接的客户端sessionID集合，用于检测是否存在此sessionID
    private final Set<String> registeredClient = new HashSet<>();
    
    // 已连接的客户端的sessionID与channel映射
    private final Map<String, Channel> connectedClient = new HashMap<>();
    
    // uri 与 路由方法、实例的映射
    private final Map<String, Router> router = new HashMap<>();
    
    // 服务器配置对象
    private SimpleNettyConfig simpleNettyConfig;
    
    private SimpleNettyContext() {}
    
    public static SimpleNettyContext init(SimpleNettyConfig simpleNettyConfig){
        simpleNettyContext.simpleNettyConfig = simpleNettyConfig;
        return simpleNettyContext;
    }
    
    public static SimpleNettyContext init(){
        simpleNettyContext.simpleNettyConfig = SimpleNettyConfig.builder().build();
        return simpleNettyContext;
    }
    
    public void run(){
        ContextHelper.runServer(this);
    }
}
